package com.fjp.heartguard.ui.report

import android.os.Bundle
import android.view.View
import androidx.viewpager.widget.ViewPager
import com.fjp.heartguard.app.base.BaseFragment
import com.fjp.heartguard.app.eventViewModel
import com.fjp.heartguard.app.util.CacheUtil
import com.fjp.heartguard.databinding.FragmentReportsBinding
import com.fjp.heartguard.ui.report.adapter.ReportFragmentPagerAdapter
import com.fjp.heartguard.viewmodel.ReportsViewModel

class ReportsFragment : BaseFragment<ReportsViewModel, FragmentReportsBinding>() {
    lateinit var adapter:ReportFragmentPagerAdapter
    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewModel = mViewModel
        mDatabind.click = ClickProxy()
        adapter=ReportFragmentPagerAdapter(childFragmentManager)
        mDatabind.viewpager.adapter = adapter
        mDatabind.viewpager.offscreenPageLimit=2
        val olderINfoList = CacheUtil.getOlderInfoList()
        if(olderINfoList.size==0){
            mDatabind.localBg.visibility = View.GONE
            mDatabind.localTv.visibility = View.GONE
            mDatabind.localNumberTv.visibility = View.GONE
        }else{
            mDatabind.localBg.visibility = View.VISIBLE
            mDatabind.localTv.visibility = View.VISIBLE
            mDatabind.localNumberTv.visibility = View.VISIBLE
        }
        mDatabind.localNumberTv.text = if(olderINfoList.size>0) olderINfoList.size.toString() else ""
        mDatabind.viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                if (position == 0) {
                    mDatabind.mineBg.visibility = View.VISIBLE
                    mDatabind.allBg.visibility = View.GONE
                    mDatabind.localBg.visibility = View.GONE
                } else if(position == 1){
                    mDatabind.mineBg.visibility = View.GONE
                    mDatabind.localBg.visibility = View.GONE
                    mDatabind.allBg.visibility = View.VISIBLE
                }else{
                    mDatabind.mineBg.visibility = View.GONE
                    mDatabind.allBg.visibility = View.GONE
                    mDatabind.localBg.visibility = View.VISIBLE
                }
            }

            override fun onPageScrollStateChanged(state: Int) {
            }

        })
    }

    override fun createObserver() {
        super.createObserver()
        eventViewModel.mineReportsAccount.observeInFragment(this) {
            mDatabind.mineNumberTv.text = it.toString()
        }
        eventViewModel.allReportsAccount.observeInFragment(this) {
            mDatabind.allNumberTv.text = it.toString()
        }

        eventViewModel.updateLocalAllReport.observeInFragment(this) {
            adapter.removeLocalFragment()
            mDatabind.localBg.visibility = View.GONE
            mDatabind.localTv.visibility = View.GONE
            mDatabind.localNumberTv.visibility = View.GONE
            mDatabind.viewpager.currentItem=1

        }

        eventViewModel.updateLocalReport.observeInFragment(this) {
            val olderINfoList = CacheUtil.getOlderInfoList()
            mDatabind.localNumberTv.text = if(olderINfoList.size>0) olderINfoList.size.toString() else ""
        }


        eventViewModel.localReport.observeInFragment(this) {
            if(adapter.count==2){
                adapter.addFragment(ReportLocalFragment())
                mDatabind.localBg.visibility = View.VISIBLE
                mDatabind.localTv.visibility = View.VISIBLE
                mDatabind.localNumberTv.visibility = View.VISIBLE
                mDatabind.viewpager.currentItem=2
                val olderINfoList = CacheUtil.getOlderInfoList()
                mDatabind.localNumberTv.text = if(olderINfoList.size>0) olderINfoList.size.toString() else ""
            }
        }
    }

    inner class ClickProxy {
        fun clickMine() {
            mDatabind.mineBg.visibility = View.VISIBLE
            mDatabind.allBg.visibility = View.GONE
            mDatabind.localBg.visibility = View.GONE
            mDatabind.viewpager.currentItem=0
        }

        fun clickAll() {
            mDatabind.mineBg.visibility = View.GONE
            mDatabind.localBg.visibility = View.GONE
            mDatabind.allBg.visibility = View.VISIBLE
            mDatabind.viewpager.currentItem=1
        }

        fun clickLocal() {
            mDatabind.mineBg.visibility = View.GONE
            mDatabind.allBg.visibility = View.GONE
            mDatabind.localBg.visibility = View.VISIBLE
            mDatabind.viewpager.currentItem=2
        }


    }
}